/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mailctrl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Administrator
 */
public class MailOperPool {

    String appName;
    String appPath;
    Date sendDate;
    Element root;
    Document document = null;
    String logfile;
    public void setAppName(String app) {
        this.appName = app;
    }

    public void setAppPath(String path) {
        this.appPath = path;
    }

    public void setSendDate(Date sddt) {
        this.sendDate = sddt;
    }
    public void setLogFile(String logfile)
    {
        this.logfile = logfile;
    }

    public Element packageOperation() {
        Element oneElement = new Element("Case");
        oneElement.addContent(new Element("Date").setText(sendDate.toString()));
        oneElement.addContent(new Element("Name").setText(appName));
        oneElement.addContent(new Element("Path").setText(appPath));
        return oneElement;
    }

    public Element packageOperation(Date pDate, String pName, String pPath) {
        Element oneElement = new Element("Case");
        oneElement.addContent(new Element("Date").setText(pDate.toString()));
        oneElement.addContent(new Element("Name").setText(pName));
        oneElement.addContent(new Element("Path").setText(pPath));
        return oneElement;
    }

    public void pushElement(Element et) {
        root.addContent(et);
    }
    public void closeLogFile()
    {
         try {
            Format format = Format.getCompactFormat();
            format.setEncoding("gb2312");           //设置xml文件的字符为gb2312
            format.setIndent("    ");               //设置xml文件的缩进为4个空格
            XMLOutputter XMLOut = new XMLOutputter(format);
            XMLOut.output(document, new FileOutputStream(logfile));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public void openLogFile() {
        SAXBuilder builder = new SAXBuilder(false);
        File file = new File(logfile);
        if (!file.exists()) {
            try {
                file.createNewFile();
                root = new Element("root");
                document = new Document(root);
            } catch (IOException ex) {
                Logger.getLogger(MailOperPool.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                document = (Document) builder.build(file);
                root = document.getRootElement();
            } catch (JDOMException ex) {
                Logger.getLogger(MailOperPool.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MailOperPool.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void openLogFile(String logname) {
        SAXBuilder builder = new SAXBuilder(false);
        File file = new File(logname);
        if (!file.exists()) {
            try {
                file.createNewFile();
                root = new Element("root");
                document = new Document(root);
            } catch (IOException ex) {
                Logger.getLogger(MailOperPool.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                document = (Document) builder.build(file);
                root = document.getRootElement();
            } catch (JDOMException ex) {
                Logger.getLogger(MailOperPool.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MailOperPool.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void runCase()
    {
        
    }
}
